package com.wy.android.mp.location;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.wy.android.mp.R;


/**
 *根据坐标来查询界面 
 * */


public class QueryPositionByLLActivity extends MapActivity {
	
	
	private LinearLayout layout;
	private MapView mapView;
	private ZoomControls controls;
	private LocationManager locationManager;
	private List<Overlay> mapOverlays;      //标志
	private Drawable drawable;
	private MyPositionItemizedOverlay overlay;
	
	
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.query_position_byll);
	        layout=(LinearLayout) findViewById(R.id.zoomView2);
	        mapView=(MapView) findViewById(R.id.mapview2);
	        controls=(ZoomControls) mapView.getZoomControls();
	        layout.addView(controls);
	        
	        double lat=39.92;
	        double lng=116.46;
	        locate(lat,lng);
	        
	 }
	
	
   //获取当前经纬度信息
	private void locate(double lat, double lng) {
		if(lat==0&&lng==0){
			lat=39.92;
			lng=116.46;
		}
		
		MapController controller=mapView.getController();
		GeoPoint geoPoint=new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
		//定位
		controller.animateTo(geoPoint);
		
        drawable=this.getResources().getDrawable(R.drawable.androidmarker);
        overlay=new MyPositionItemizedOverlay(drawable);
        mapOverlays=mapView.getOverlays();
        OverlayItem item=new OverlayItem(geoPoint,"","");
        overlay.addOverlay(item);
        mapOverlays.add(overlay);
	}



	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	//自定义对话框
	private void customDialog(){
		try{
		LayoutInflater inflater=LayoutInflater.from(QueryPositionByLLActivity.this);
		
		View myview=inflater.inflate(R.layout.custom_dialog,null);
		final EditText lngEditText=(EditText) myview.findViewById(R.id.lngEditText);
		final EditText latEditText=(EditText) myview.findViewById(R.id.latEditText);
		
		AlertDialog.Builder builder=new Builder(this);
		
		builder.setView(myview);
		builder.setTitle("请输入经纬度坐标！");
	    builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
			
		
			public void onClick(DialogInterface dialog, int which) {
				
				locate(Double.parseDouble(latEditText.getText().toString()),Double.parseDouble(lngEditText.getText().toString()));
				
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
			
			}
		});
		
		builder.show();
		
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		menu.add(0, 1, 0, "显示输入坐标对话框");
		
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case 1:
			customDialog();
			return true;
		}
		return false;
	}

	
	
}
